package stark.app.webserver;

import android.content.Context;
import android.content.Intent;

/**
 * <pre>
 *     author : cfans
 *     time   : 2020/11/21
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class WebServerManager {

    interface  WebServerManagerCallback{
        void onServerStart(String ip);
        void onServerError(String error);
        void onServerStop();
    }

    /**
     * 静态内部类单例
     */
    private static class Holder {
        private static WebServerManager sInstance = new WebServerManager();
    }

    public static WebServerManager shared() {
        return Holder.sInstance;
    }

    private WebServerManager(){}

    Intent mServiceIntent;
    WebServerManagerCallback mCallback;

    public void startServer(Context context,WebServerManagerCallback callback) {
        mCallback = callback;
        mServiceIntent = new Intent(context, WebServerService.class);
        context.startService(mServiceIntent);
    }

    public void stopServer(Context context) {
        mCallback = null;
        context.stopService(mServiceIntent);
    }

    public void onServerStart(Context context, String hostAddress) {
        if (mCallback != null){
            mCallback.onServerStart(hostAddress);
        }
    }

    public void onServerError(Context context, String error) {
        if (mCallback != null){
            mCallback.onServerError(error);
        }
    }

    public void onServerStop(Context context) {
        if (mCallback != null){
            mCallback.onServerStop();
        }
    }
}